package com.ruoyi.work.controller;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import com.ruoyi.work.domain.MapRoadlinks;
import com.ruoyi.work.service.IMapRoadlinksService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 路线Controller
 * 
 * @author ruoyi
 * @date 2025-09-28
 */
@Tag(name = "路线")
@RestController
@RequestMapping("/work/roadlinks")
public class MapRoadlinksController extends BaseController
{
    @Autowired
    private IMapRoadlinksService mapRoadlinksService;

    /**
     * 查询路线列表
     */
    @Operation(summary = "查询路线列表")
    @PreAuthorize("@ss.hasPermi('work:roadlinks:list')")
    @GetMapping("/list")
    public TableDataInfo list(MapRoadlinks mapRoadlinks)
    {
        startPage();
        List<MapRoadlinks> list = mapRoadlinksService.selectMapRoadlinksList(mapRoadlinks);
        return getDataTable(list);
    }

    /**
     * 导出路线列表
     */
    @Operation(summary = "导出路线列表")
    @PreAuthorize("@ss.hasPermi('work:roadlinks:export')")
    @Log(title = "路线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MapRoadlinks mapRoadlinks)
    {
        List<MapRoadlinks> list = mapRoadlinksService.selectMapRoadlinksList(mapRoadlinks);
        ExcelUtil<MapRoadlinks> util = new ExcelUtil<MapRoadlinks>(MapRoadlinks.class);
        util.exportExcel(response, list, "路线数据");
    }

    /**
     * 获取路线详细信息
     */
    @Operation(summary = "获取路线详细信息")
    @PreAuthorize("@ss.hasPermi('work:roadlinks:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mapRoadlinksService.selectMapRoadlinksById(id));
    }

    /**
     * 新增路线
     */
    @Operation(summary = "新增路线")
    @PreAuthorize("@ss.hasPermi('work:roadlinks:add')")
    @Log(title = "路线", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MapRoadlinks mapRoadlinks)
    {
        return toAjax(mapRoadlinksService.insertMapRoadlinks(mapRoadlinks));
    }

    /**
     * 修改路线
     */
    @Operation(summary = "修改路线")
    @PreAuthorize("@ss.hasPermi('work:roadlinks:edit')")
    @Log(title = "路线", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MapRoadlinks mapRoadlinks)
    {
        return toAjax(mapRoadlinksService.updateMapRoadlinks(mapRoadlinks));
    }

    /**
     * 删除路线
     */
    @Operation(summary = "删除路线")
    @PreAuthorize("@ss.hasPermi('work:roadlinks:remove')")
    @Log(title = "路线", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mapRoadlinksService.deleteMapRoadlinksByIds(ids));
    }

    /**
     * 导入路线
     */
    @Operation(summary = "导入路线")
    @Log(title = "路线", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(@RequestBody Map<String,String> data) throws Exception
    {
        boolean updateSupport = true;
        String filePath = RuoYiConfig.getProfile()+data.get("path").toString().replaceAll("/profile","");
        String message = "导入失败";
        try (InputStream input = new FileInputStream(filePath)) {
            // 操作流
            ExcelUtil<MapRoadlinks> util = new ExcelUtil<MapRoadlinks>(MapRoadlinks.class);
            List<MapRoadlinks> mapRoadlinksList = util.importExcel(input);
            message = mapRoadlinksService.importMapRoadlinks(mapRoadlinksList, updateSupport);
        }catch (Exception e) {
            message = "打开上传文件失败";
        }
        
        return AjaxResult.success(message);
    }
}
